package handler

import (
	"2301house/api/request"
	"2301house/api/resp"
	"2301house/internal/dao"
	"2301house/internal/model"
	"github.com/gin-gonic/gin"
)

func AdminOrderList(c *gin.Context) {
	var req request.OrderList
	if err := c.ShouldBind(&req); err != nil {
		resp.Error(c, 10000, "参数错误", err.Error())
		return
	}
	var order []model.Order
	result := dao.GetListByFields(&model.Order{}, &order)
	if !result {
		resp.Error(c, 10000, "查看订单失败", nil)
		return
	}
	resp.Success(c, "查看订单成功", order)
}

func AdminUpdateOrderType(c *gin.Context) {
	var req request.AdminUpdateOrderType
	if err := c.ShouldBind(&req); err != nil {
		resp.Error(c, 10000, "参数错误", err.Error())
		return
	}
	var user model.User
	dao.GetOneById(c.GetUint("userId"), &user)
	if !(user.UType == 3) {
		resp.Error(c, 10000, "没有权限修改", nil)
		return
	}
	var order model.Order
	dao.GetOneByFields(&model.Order{
		Id:    req.Id,
		OType: req.OType,
	}, &order)
	if order.Id != 0 {
		resp.Error(c, 10000, "当前订单不存在，无法修改", nil)
		return
	}
	result := dao.Update(&model.Order{
		Id:    req.Id,
		OType: 1, //当房屋状态为1时，房源待审核
	})
	if !result {
		resp.Error(c, 10000, "修改订单状态失败", nil)
		return
	}
	resp.Success(c, "修改订单状态成功", nil)
}
